/**
  ******************************************************************************
  * @file    Libraries/Device/TS32Fx/TS32Fx_LL_Driver/src/ts32fx_ll_comp.c
  * @author  TOPSYS Application Team
  * @version V1.0.0
  * @date    02-11-2018
  * @brief   This file contains all the comp LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2018 TOPSYS</center></h2>
  *
  *
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "include.h"

/** @addtogroup TS32Fx_StdPeriph_Driver TS32Fx Driver
  * @{
  */
  
/** @defgroup comp_interface_gr COMP Driver
  * @ingroup  TS32Fx_StdPeriph_Driver
  * @{
  */

/** @addtogroup COMP_LL_Driver COMP LL Driver
  * @ingroup  comp_interface_gr
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

  
/** @defgroup COMP_LL_Interrupt COMP LL Interrupt Handle function
  * @ingroup  COMP_LL_Driver
  * @brief   COMP LL Interrupt Handle function
  * @{
  */



/**
  * @}
  */

/** @defgroup COMP_LL_Inti_Cfg COMP LL Initialization And Configuration
  * @ingroup  COMP_LL_Driver
  * @brief    COMP LL Initialization And Configuration
  * @{
  */

  /**
  * @brief  comp init
  * @param  channel : enummerate of comparator0 or comparator1
  * @param  p_init  : pointer to the init stuct TYPE_LL_COMP_INIT
  * @retval None
  */
void ll_comp_init(TYPE_ENUM_LL_COMP_CH channel,TYPE_LL_COMP_INIT *p_init)
{
    u32 comp_con, adkey_sta;
    if(channel == LL_COMP_CH0)
    {
        comp_con = COMP->CON;
        comp_con = (comp_con & (~LL_COMP_CON0_CH_PSEL(ALL_WORD_FF))) | LL_COMP_CON0_CH_PSEL(p_init->comp0_positive_sel);
        comp_con = (comp_con & (~LL_COMP_CON0_CH_NSEL(ALL_WORD_FF))) | LL_COMP_CON0_CH_NSEL(p_init->comp0_negative_sel);
        comp_con = (comp_con & (~LL_COMP_CON0_FILT_NUM(ALL_WORD_FF))) | LL_COMP_CON0_FILT_NUM(p_init->filt_num);
        comp_con = (comp_con & (~LL_COMP_CON0_HY_SEL(ALL_WORD_FF))) | LL_COMP_CON0_HY_SEL(p_init->hy_mode);
        if(p_init->invert_en)
        {
            comp_con |= LL_COMP_CON0_INV_EN;
        }
        else
        {
            comp_con &= ~LL_COMP_CON0_INV_EN;
        }
        
        if(p_init->po_en)
        {
            comp_con |= LL_COMP_CON0_POEN;
        }
        else
        {
            comp_con &= ~LL_COMP_CON0_POEN;
        }
        
        if(p_init->lowp_en)
        {
            comp_con |= LL_COMP_CON0_LOWP;
        }
        else
        {
            comp_con &= ~LL_COMP_CON0_LOWP;
        }
        
        COMP->CON = comp_con;
    }
    else if(channel == LL_COMP_CH1)
    {
        adkey_sta = ADKEY->STA;
        adkey_sta = (adkey_sta & (~LL_COMP_CON1_CH_NSEL(ALL_WORD_FF))) | LL_COMP_CON1_CH_NSEL(p_init->comp1_positive_sel);
        ADKEY->STA = adkey_sta;
        
        comp_con = COMP->CON1;
        comp_con = (comp_con & (~LL_COMP_CON1_FILT_NUM(ALL_WORD_FF))) | LL_COMP_CON1_FILT_NUM(p_init->filt_num);
        if(p_init->invert_en)
        {
            comp_con |= LL_COMP_CON1_INV_EN;
        }
        else
        {
            comp_con &= ~LL_COMP_CON1_INV_EN;
        }
        
        COMP->CON1 = comp_con;
    }
    
}


 /**
  * @brief  comp init
  * @param  channel : enummerate of comparator0 or comparator1
  * @param  p_cfg   : pointer to the init stuct TYPE_LL_COMP_WAKEUP_CFG
  * @retval None
  */
void ll_comp_wakeup_or_irq_config(TYPE_ENUM_LL_COMP_CH channel,TYPE_LL_COMP_WAKEUP_OR_IRQ_CFG *p_cfg)
{
    u32 comp_con;
    if(channel == LL_COMP_CH0)
    {
        comp_con = COMP->CON;
        comp_con = (comp_con & (~LL_COMP_CON0_WKUP_SEL(ALL_WORD_FF))) | LL_COMP_CON0_WKUP_SEL(p_cfg->wk_irq_mode);
        if(p_cfg->wakeup_en)
        {
            comp_con |= LL_COMP_CON0_WKUP_EN;
        }
        else
        {
            comp_con &= ~LL_COMP_CON0_WKUP_EN;
        }
        if(p_cfg->irq_en)
        {
            comp_con |= LL_COMP_CON0_IE;
        }
        else
        {
            comp_con &= ~LL_COMP_CON0_IE;
        }
        COMP->CON = comp_con;
    }
    else if(channel == LL_COMP_CH1)
    {
        comp_con = COMP->CON1;
        comp_con = (comp_con & (~LL_COMP_CON1_WKUP_SEL(ALL_WORD_FF))) | LL_COMP_CON1_WKUP_SEL(p_cfg->wk_irq_mode);
        if(p_cfg->wakeup_en)
        {
            comp_con |= LL_COMP_CON1_WKUP_EN;
        }
        else
        {
            comp_con &= ~LL_COMP_CON1_WKUP_EN;
        }
        if(p_cfg->irq_en)
        {
            comp_con |= LL_COMP_CON1_IE;
        }
        else
        {
            comp_con &= ~LL_COMP_CON1_IE;
        }
        COMP->CON1 = comp_con;
    }
    
}


 /**
  * @brief  comparator0 interrupt enable
  * @param  None
  * @retval None
  */
void ll_comp0_interrupt_enable(void)
{
    COMP->CON |= LL_COMP_CON0_IE;
}

 /**
  * @brief  comparator1 interrupt enable
  * @param  None
  * @retval None
  */
void ll_comp1_interrupt_enable(void)
{
    COMP->CON1 |= LL_COMP_CON1_IE;
}

 /**
  * @brief  comparator0 interrupt disable
  * @param  None
  * @retval None
  */
void ll_comp0_interrupt_disable(void)
{
    COMP->CON &= ~LL_COMP_CON0_IE;
}

 /**
  * @brief  comparator1 interrupt disable
  * @param  None
  * @retval None
  */
void ll_comp1_interrupt_disable(void)
{
    COMP->CON1 &= ~LL_COMP_CON1_IE;
}


 /**
  * @brief  comparator0 enable
  * @param  None
  * @retval None
  */
void ll_comp0_enable(void)
{
    COMP->CON |= LL_COMP_CON0_EN;
}

 /**
  * @brief  comparator1 enable
  * @param  None
  * @retval None
  */
void ll_comp1_enable(void)
{
    COMP->CON1 |= LL_COMP_CON1_EN;
}

 /**
  * @brief  comparator0 disable
  * @param  None
  * @retval None
  */
void ll_comp0_disable(void)
{
    COMP->CON &= ~LL_COMP_CON0_EN;
}

 /**
  * @brief  comparator1 disable
  * @param  None
  * @retval None
  */
void ll_comp1_disable(void)
{
    COMP->CON1 &= ~LL_COMP_CON1_EN;
}


/**
  * @}
  */

/** @defgroup COMP_LL_Data_Transfers COMP LL Data transfers functions
  * @ingroup  COMP_LL_Driver
  * @brief    COMP LL Data transfers functions 
  * @{
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2018 TOPSYS ***** END OF FILE *****/
